const config = require('./config');

const solChain = require('./SolChain');


const {   
    Program,
    AnchorProvider,
    BN,
    web3,
    Wallet,
} = require("@coral-xyz/anchor");


const {  
    Connection,
    clusterApiUrl,
    PublicKey,
    Keypair
} = require('@solana/web3.js')

const idl = require("./idl/basic_1.json");

const {
    getKeyPairUser1, getKeyPairUser2
} = require('./MyKeypair');
const { decodeInitializePermanentDelegateInstructionUnchecked } = require('@solana/spl-token');
const opts = {
    preflightCommitment: "recent",
  };

  const programID = new PublicKey(config.local_programID);

const getProgramInfo = async () => {
    let pk = new PublicKey(programID);
    let info = await solChain.getAccountInfo(pk);
    console.log('info: ', info);

}

const getAccountInfo = async () => {
    const user2Kp = await getKeyPairUser2();
    let info = await solChain.getAccountInfo(user2Kp.publicKey);
    console.log('info: ', info);
}

const run = async () => {
    const connection = new Connection(config.nodeUrl, opts.preflightCommitment);

    const user1Kp = await getKeyPairUser1();
    const user2Kp = await getKeyPairUser2();

    const wallet = new Wallet(user1Kp);
    const wallet2 = new Wallet(user2Kp);
    const provider = new AnchorProvider(
      connection, wallet2, opts.preflightCommitment,
    )
    
    const program = new Program(idl, programID, provider);

    let myAccount = web3.Keypair.generate();

  

    let txSignature = await program.methods.initialize(new BN(1234)).accounts({
        myAccount: myAccount.publicKey,
        user: provider.wallet.publicKey,
        systemProgram: web3.SystemProgram.programId,}).signers([ user2Kp, myAccount]).rpc();

    console.log('txSignature: ', txSignature);

    const acc = await program.account.myAccount.fetch(myAccount.publicKey);
    console.log('acc: ', acc)
}

//getProgramInfo();
run();
//getAccountInfo();